package com.jyw.business.de.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.jyw.business.de.domain.vo.web.DemandProcessVO;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.de.mapper.DeDemandProcessMapper;
import com.jyw.business.de.domain.DeDemandProcess;
import com.jyw.business.de.service.IDeDemandProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 需求-需求处理流程Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Service
public class DeDemandProcessServiceImpl extends ServiceImpl<DeDemandProcessMapper,DeDemandProcess> implements IDeDemandProcessService
{
    @Resource
    private DeDemandProcessMapper deDemandProcessMapper;

    /**
     * 查询需求-需求处理流程
     * 
     * @param id 需求-需求处理流程主键
     * @return 需求-需求处理流程
     */
    @Override
    public DeDemandProcess selectDeDemandProcessById(Long id)
    {
        return deDemandProcessMapper.selectDeDemandProcessById(id);
    }

    /**
     * 查询需求-需求处理流程列表
     * 
     * @param deDemandProcess 需求-需求处理流程
     * @return 需求-需求处理流程
     */
    @Override
    public List<DeDemandProcess> selectDeDemandProcessList(DeDemandProcess deDemandProcess)
    {
        return deDemandProcessMapper.selectDeDemandProcessList(deDemandProcess);
    }

    /**
     * 新增需求-需求处理流程
     * 
     * @param deDemandProcess 需求-需求处理流程
     * @return 结果
     */
    @Override
    public int insertDeDemandProcess(DeDemandProcess deDemandProcess)
    {
        deDemandProcess.setCreateTime(DateUtils.getNowDate());
        return deDemandProcessMapper.insertDeDemandProcess(deDemandProcess);
    }

    /**
     * 修改需求-需求处理流程
     * 
     * @param deDemandProcess 需求-需求处理流程
     * @return 结果
     */
    @Override
    public int updateDeDemandProcess(DeDemandProcess deDemandProcess)
    {
        deDemandProcess.setUpdateTime(DateUtils.getNowDate());
        return deDemandProcessMapper.updateDeDemandProcess(deDemandProcess);
    }

    /**
     * 批量删除需求-需求处理流程
     * 
     * @param ids 需要删除的需求-需求处理流程主键
     * @return 结果
     */
    @Override
    public int deleteDeDemandProcessByIds(Long[] ids)
    {
        return deDemandProcessMapper.deleteDeDemandProcessByIds(ids);
    }

    /**
     * 删除需求-需求处理流程信息
     * 
     * @param id 需求-需求处理流程主键
     * @return 结果
     */
    @Override
    public int deleteDeDemandProcessById(Long id)
    {
        return deDemandProcessMapper.deleteDeDemandProcessById(id);
    }

    @Override
    public void saveProcess(Long id, String operState, Object obj, Long user) {
        DeDemandProcess deDemandProcess = new DeDemandProcess();
        deDemandProcess.setDemandId(id);
        deDemandProcess.setOperState(operState);
        if (!ObjectUtils.isEmpty(obj)) {
            deDemandProcess.setProcessValue(JSONObject.toJSONString(obj));
        }
        deDemandProcess.setCreateUser(user);
        deDemandProcess.setCreateTime(DateUtils.getNowDate());
        save(deDemandProcess);
    }

    @Override
    public void saveProcess(Long id, String operState, Object obj) {
        saveProcess(id,operState,obj, SecurityUtils.getUserId());
    }

    @Override
    public List<DemandProcessVO> getProcessInfo(Long id) {
        List<DeDemandProcess> list = lambdaQuery()
                .eq(DeDemandProcess::getDemandId, id)
                .orderByDesc(DeDemandProcess::getId)
                .list();
        return list.stream().map(data -> ConversionUtil.conversion(new DemandProcessVO(data),DemandProcessVO.class)).collect(Collectors.toList());
    }
}
